package com.situ.mall;

public class digui {
    public static void main(String[] args) {
        int result = factorial(5);
        System.out.println(result);
    }

    /**
     * n!=n*(n-1)*(n-2)*(n-3)*...*3*2*1
     * 5!=5*4*3*2*1
     * factorial(5)=5*factorial(4)      5*24=120
     * factorial(4)=4*factorial(3)      4*6=24
     * factorial(3)=3*factorial(2)      3*2=6
     * factorial(2)=2*factorial(1)      2*1=2
     * factorial(1)=1                   1
     factorial(n)=n*factorial(n-1)
     */
    public static int factorial(int n) {
        if (n == 1) {//递归退出的条件
            return 1;
        }
        return n * factorial(n - 1);
    }
}
